import {PlotTypes} from '../../enum/PlotTypes';
import {Point} from '../../type';
import * as PlotUtils from '../../utils';
import * as Constants from '../../constants';
import {
    calculateNewCoordinateOfRotate,
    calculateNewCoordinateOfScale,
    calculateNewCoordinateOfTranslate
} from '../../utils';

// @ts-ignore
export default class StraightArrow extends ol.geom.LineString {
    public params: any;
    public isPlot: boolean = true;
    public fixPointCount: number = 2;
    public maxArrowLength: number = 3000000;
    public arrowLengthScale: number = 5;

    private readonly _type: PlotTypes;
    private points: Array<Point>;

    constructor(coordinates: any, points: Array<Point>, params: any) {
        super([]);
        this._type = PlotTypes.STRAIGHT_ARROW;
        this.points = [];
        this.params = params;
        if (points && points.length > 0) {
            this.setPoints(points);
        } else if (coordinates && coordinates.length > 0) {
            // @ts-ignore
            this.setCoordinates(coordinates);
        }
    }

    public transform(type: string, params: any): any {
        if (type === 'scale') {
            const {scaleX, scaleY, centerX, centerY, paths} = params;
            const newPoints = calculateNewCoordinateOfScale([scaleX, scaleY], [centerX, centerY], paths);
            this.setPoints(newPoints);
        } else if (type === 'rotate') {
            const {angle, centerX, centerY, paths} = params;
            const newPaths = calculateNewCoordinateOfRotate(paths, [centerX, centerY], angle);
            this.setPoints(newPaths);
        } else if (type === 'translate') {
            const {deltaX, deltaY, centerX, centerY, paths} = params;
            const newPoints = calculateNewCoordinateOfTranslate(paths, [deltaX, deltaY]);
            this.setPoints(newPoints);
        }
    }

    /**
     * 获取标绘类型
     * @return {PlotTypes}
     */
    public getPlotType(): PlotTypes {
        return this._type;
    }

    /**
     * 获取坐标点
     * @return {Array<Point>}
     */
    public getPoints(): Array<Point> {
        return this.points.slice(0);
    }

    /**
     * 获取点数理
     * @return {number}
     */
    public getPointCount(): number {
        return this.points.length;
    }

    /**
     * 更新当前坐标
     * @param point
     * @param index
     */
    public updatePoint(point: Point, index: number): void {
        if (index >= 0 && index < this.points.length) {
            this.points[index] = point;
            this.generate();
        }
    }

    private updateLastPoint(point: Point): void {
        this.updatePoint(point, this.points.length - 1);
    }

    /**
     * 完成绘制
     */
    public finishDrawing(): void {
    }

    private setPoints(value: Array<Point>): void {
        this.points = !value ? [] : value;
        if (this.points.length >= 1) {
            this.generate();
        }
    }

    private generate(): void {
        try {
            let count: number = this.getPointCount();
            if (count < 2) {
                return;
            } else {
                let points: Array<Point> = this.getPoints();
                let [pnt1, pnt2]: [pnt1: Point, pnt2: Point] = [points[0], points[1]];
                let distance: number = PlotUtils.mathDistance(pnt1, pnt2);
                let len: number = distance / this.arrowLengthScale;
                len = ((len > this.maxArrowLength) ? this.maxArrowLength : len);
                let leftPnt: Point = PlotUtils.getThirdPoint(pnt1, pnt2, Math.PI / 6, len, false);
                let rightPnt: Point = PlotUtils.getThirdPoint(pnt1, pnt2, Math.PI / 6, len, true);
                // @ts-ignore
                this.setCoordinates([pnt1, pnt2, leftPnt, pnt2, rightPnt]);
            }
        } catch (e) {
            console.log(e);
        }
    }
}
